/**
 * 情绪色彩系统
 * 根据情绪状态生成对应的色彩主题
 */

/**
 * 情绪色彩映射
 */
export const emotionColorMap = {
  '喜而不狂': {
    primary: '#f59e0b', // 温暖金色
    secondary: '#fbbf24', // 亮金色
    background: 'linear-gradient(135deg, #fef3c7 0%, #fde68a 50%, #f59e0b 100%)',
    accent: '#d97706',
    text: '#92400e',
    description: '温暖的金色调，象征喜悦与希望'
  },
  '哀而不伤': {
    primary: '#6b7280', // 温和灰色
    secondary: '#9ca3af', // 浅灰色
    background: 'linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 50%, #d1d5db 100%)',
    accent: '#4b5563',
    text: '#374151',
    description: '柔和的灰色调，表达淡淡的忧伤'
  },
  '思而不忧': {
    primary: '#3b82f6', // 深蓝色
    secondary: '#60a5fa', // 亮蓝色
    background: 'linear-gradient(135deg, #dbeafe 0%, #bfdbfe 50%, #93c5fd 100%)',
    accent: '#1d4ed8',
    text: '#1e40af',
    description: '宁静的蓝色调，代表深度思考'
  },
  '淡然自若': {
    primary: '#10b981', // 翠绿色
    secondary: '#34d399', // 浅绿色
    background: 'linear-gradient(135deg, #d1fae5 0%, #a7f3d0 50%, #6ee7b7 100%)',
    accent: '#059669',
    text: '#047857',
    description: '清新的绿色调，体现内心平静'
  },
  '诗意盎然': {
    primary: '#8b5cf6', // 紫色
    secondary: '#a78bfa', // 浅紫色
    background: 'linear-gradient(135deg, #ede9fe 0%, #ddd6fe 50%, #c4b5fd 100%)',
    accent: '#7c3aed',
    text: '#6d28d9',
    description: '优雅的紫色调，展现诗意美感'
  },
  '略有遗憾': {
    primary: '#ef4444', // 温和红色
    secondary: '#f87171', // 浅红色
    background: 'linear-gradient(135deg, #fee2e2 0%, #fecaca 50%, #fca5a5 100%)',
    accent: '#dc2626',
    text: '#b91c1c',
    description: '温和的红色调，表达轻微的遗憾'
  }
}

/**
 * 默认色彩主题
 */
export const defaultColorTheme = {
  primary: '#d97706',
  secondary: '#f59e0b',
  background: 'linear-gradient(135deg, #f8f4e8 0%, #f2ecd8 100%)',
  accent: '#b45309',
  text: '#92400e',
  description: '经典的国风金色调'
}

/**
 * 根据情绪获取色彩主题
 * @param {string} emotion - 情绪标签
 * @returns {Object} - 色彩主题对象
 */
export const getEmotionColors = (emotion) => {
  return emotionColorMap[emotion] || defaultColorTheme
}

/**
 * 生成CSS变量字符串
 * @param {Object} colorTheme - 色彩主题
 * @returns {string} - CSS变量字符串
 */
export const generateCSSVariables = (colorTheme) => {
  return `
    --emotion-primary: ${colorTheme.primary};
    --emotion-secondary: ${colorTheme.secondary};
    --emotion-background: ${colorTheme.background};
    --emotion-accent: ${colorTheme.accent};
    --emotion-text: ${colorTheme.text};
  `
}

/**
 * 应用情绪色彩到页面
 * @param {string} emotion - 情绪标签
 */
export const applyEmotionColors = (emotion) => {
  const colorTheme = getEmotionColors(emotion)
  const cssVariables = generateCSSVariables(colorTheme)
  
  // 创建或更新样式标签
  let styleElement = document.getElementById('emotion-colors')
  if (!styleElement) {
    styleElement = document.createElement('style')
    styleElement.id = 'emotion-colors'
    document.head.appendChild(styleElement)
  }
  
  styleElement.textContent = `
    :root {
      ${cssVariables}
    }
    
    .emotion-background {
      background: ${colorTheme.background};
      transition: background 2s ease-in-out;
    }
    
    .emotion-text {
      color: ${colorTheme.text};
      transition: color 1s ease-in-out;
    }
    
    .emotion-accent {
      color: ${colorTheme.accent};
      transition: color 1s ease-in-out;
    }
    
    .emotion-border {
      border-color: ${colorTheme.primary};
      transition: border-color 1s ease-in-out;
    }
    
    .emotion-shadow {
      box-shadow: 0 10px 25px ${colorTheme.primary}20;
      transition: box-shadow 1s ease-in-out;
    }
  `
}

/**
 * 重置为默认色彩
 */
export const resetToDefaultColors = () => {
  applyEmotionColors(null)
}

/**
 * 获取情绪色彩的RGB值（用于动画）
 * @param {string} emotion - 情绪标签
 * @returns {Object} - RGB色彩值
 */
export const getEmotionRGB = (emotion) => {
  const colorTheme = getEmotionColors(emotion)
  
  // 简单的十六进制转RGB函数
  const hexToRgb = (hex) => {
    const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
    return result ? {
      r: parseInt(result[1], 16),
      g: parseInt(result[2], 16),
      b: parseInt(result[3], 16)
    } : null
  }
  
  return {
    primary: hexToRgb(colorTheme.primary),
    secondary: hexToRgb(colorTheme.secondary),
    accent: hexToRgb(colorTheme.accent)
  }
}

export default {
  emotionColorMap,
  defaultColorTheme,
  getEmotionColors,
  generateCSSVariables,
  applyEmotionColors,
  resetToDefaultColors,
  getEmotionRGB
}
